package com.jzg.jcptadmin.adpter;

import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jzg.jcptadmin.R;
import com.jzg.jcptadmin.Utils.ScreenUtils;
import com.jzg.jcptadmin.base.BaseActivity;
import com.jzg.jcptadmin.data.vo.CarDetails;
import com.jzg.template.refreshrecycleview.RecycleViewBaseAdapter;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

import cn.pedant.SweetAlert.SweetAlertDialog;

/**
 * @author: voiceofnet
 * email: pengkun@jingzhengu.com
 * phone:18101032717
 * @time: 2016/5/17 11:52
 * @desc:固定顺序图片适配器
 */
public class StableModifyAdapter extends RecycleViewBaseAdapter {
    private BaseActivity context;
    private List<CarDetails.CarPicListEntity> imageList;
    private int[] defaultPics;
    private int imageWidth = 0;
    private int imageHeight = 0;

    public StableModifyAdapter(BaseActivity context, List<CarDetails.CarPicListEntity> info, int[] defaultPics) {
        this.context = context;
        this.imageList = info;
        this.defaultPics = defaultPics;
        imageWidth = (ScreenUtils.getScreenWidth(context)-ScreenUtils.dip2px(context,30))/3;
        imageHeight = imageWidth*133/200;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new MyViewHolder(LayoutInflater.from(context).inflate(R.layout.item_add_pic_new, null));
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        super.onBindViewHolder(holder, position);
        MyViewHolder myViewHolder = (MyViewHolder) holder;
        CarDetails.CarPicListEntity imageItem = imageList.get(position);
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(imageWidth,imageHeight);
        params.leftMargin=ScreenUtils.dip2px(context,5);
        params.rightMargin=ScreenUtils.dip2px(context,5);
        myViewHolder.pic.setLayoutParams(params);
        String imgUrl = imageItem.getPathBig();
        if(TextUtils.isEmpty(imgUrl)){
            ImageLoader.getInstance().displayImage("drawable://" + defaultPics[position], myViewHolder.pic);
            myViewHolder.pic_delete.setVisibility(View.GONE);
        }else{
            myViewHolder.pic_delete.setVisibility(View.VISIBLE);
            if(imgUrl.startsWith("http")){
                ImageLoader.getInstance().displayImage(imgUrl, myViewHolder.pic);
            }else{
                ImageLoader.getInstance().displayImage("file://"+imgUrl, myViewHolder.pic);
            }

        }
        myViewHolder.textView.setText(imageItem.getItemName());
        myViewHolder.pic_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new SweetAlertDialog(context, SweetAlertDialog.WARNING_TYPE)
                        .setTitleText("提示")
                        .setContentText("您确定要删除该照片吗？")
                        .setConfirmText("是的").setCancelText("取消")
                        .setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
                            @Override
                            public void onClick(SweetAlertDialog sDialog) {
                                sDialog.dismissWithAnimation();
                                int i = holder.getAdapterPosition();
                                imageList.get(i).setPath("");
                                imageList.get(i).setPathBig("");
                                imageList.get(i).setId(0);
                                notifyDataSetChanged();
                            }
                        }).show();
            }
        });

    }


    @Override
    public int getItemCount() {
        return imageList.size();

    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        ImageView pic;
        ImageView pic_delete;
        TextView textView;

        public MyViewHolder(View view) {
            super(view);
            pic = (ImageView) view.findViewById(R.id.pic_item);
            pic_delete = (ImageView) view.findViewById(R.id.pic_delete);
            textView = (TextView) view.findViewById(R.id.name_text);
        }
    }
}
